package com.gouldzd.jobs;

import com.gouldzd.constant.RedisConstant;
import com.gouldzd.utils.AliyunUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/*
 * @Author zhangdan
 * @Description //TODO 定时任务管理
 * @Date 17:29 2020/5/1
 * @Param
 * @return
 **/
@Component
public class ClearImgJob {

    @Autowired
    private JedisPool jedisPool;

    @Scheduled(cron = "0/10 * * * * ?")
    public void clearImg(){
        //根据Redis中保存的两个set集合进行差值计算,获得垃圾图片名称集合
        //Set<String> sdiff(String ... key):计算两个集合的差值
        Set<String> set = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if(set != null){
            for (String picName : set) {
                //删除阿里云服务器上的图片
                AliyunUtils.deleteFileFromAliyun(picName);
                //从redis集合中删除图片名称
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,picName);
                System.out.println("自定义任务执行,清理垃图片"+picName);
            }
        }
    }
}
